package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BusLeave;

import cn.wolfcode.car.business.mapper.BusLeaveMapper;
import cn.wolfcode.car.business.query.BusLeaveQuery;
import cn.wolfcode.car.business.service.IBusLeaveService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class BusLeaveServiceImpl implements IBusLeaveService {

    @Autowired
    private BusLeaveMapper busLeaveMapper;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;




    @Override
    public TablePageInfo<BusLeave> query(BusLeaveQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BusLeave>(busLeaveMapper.selectForList(qo));
    }


    @Override
    public void save(BusLeave busLeave) {

        busLeaveMapper.insert(busLeave);
    }

    @Override
    public BusLeave get(Long id) {
        return busLeaveMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(BusLeave busLeave) {

        busLeaveMapper.updateByPrimaryKey(busLeave);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {

            busLeaveMapper.deleteByPrimaryKey(dictId);

        }
    }

    @Override
    public List<BusLeave> list() {
        return busLeaveMapper.selectAll();
    }

   @Override
    public boolean checkNameExsit(String name) {
        return busLeaveMapper.selectByName(name) != null;
    }

    @Override
    @Transactional
    public void startAudit(BusLeave busLeave, Integer mgr, Integer hr) {
        //发起审核
        //设置id
        busLeave.setId(ShiroUtils.getUserId());
        busLeave.setAuditid(mgr);
        busLeave.setStatus(BusLeave.STATUS_INIT);
        //插入数据
        busLeaveMapper.insert(busLeave);
        //设置流程变量 MGR HR
        HashMap<String , Object> map = new HashMap<>();
        map.put("mgr",mgr);
        map.put("hr",hr);
        //开启流程实例
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("leaveProcess", busLeave.getId().toString(), map);
    }


}
